// pages/cash/cashRecord.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,
        // 导航栏数据
        styledata: {
            "bg_color": "#fff",
            "color": "#000",
            "name": "积分兑换记录",
            "leftflag": true,
            // "homeflag": true,
        },

        // 记录
        cashlist: [],

        // 状态描述
        descrition: ["生成订单", "兑换成功", "兑换失败"],

        innerBoxHeight: 0,
        noMore: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        const that = this
        wx.getSystemInfo({
            success(res) {
                console.log('机型信息', res.screenHeight);
                that.setData({
                    innerBoxHeight: res.screenHeight - App.globalData.paddingTop
                })
            }
        })

        this.setData({
            uid: App.globalData.uid,
        })

        this.getWithdrawList()
    },

    page: 1,
    limit: 10,

    async getWithdrawList() {

        // 判断是否已经到底
        if (this.data.noMore) {
            return
        }

        util.showLoading('正在加载')

        let params = {
            uid: this.data.uid,
            page: this.page,
            limit: this.limit
        }

        // 如果为下拉刷新，重新开辟新数组填充数据
        let ispullrefresh = this.data.ispullrefresh

        let list = ispullrefresh ? [] : this.data.cashlist
        let res = await request.post(api.pToCashList, params)

        // 页数+1
        this.page = this.page + 1

        if (res.length == 0) {
            this.setData({
                noMore: true
            })
            // 如果是下拉刷新 需要清空原列表
            if (ispullrefresh) {
                this.setData({
                    cashlist: [],
                })
            }
            util.hideLoading()
            return true
        }

        res.forEach(item => {
            item.amount = item.amount.toFixed(2)
            item.balance = (item.balance / 100).toFixed(2)
        });

        let newlist = list.concat(res)

        this.setData({
            cashlist: newlist,
        })
        util.hideLoading()
        return true
    },

    // 累计分账 下拉刷新
    pullRefresh(e) {
        this.page = 1
        this.setData({
            noMore: false,
            ispullrefresh: true,
            pullflag: true,
        })

        setTimeout(async () => {
            let flag = await this.getWithdrawList()
            if (flag) {
                this.setData({
                    pullflag: false,
                    ispullrefresh: false
                })
            }
        }, 1500);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})